package com.maimao.search.service;

import com.maimao.search.utils.EsUtil;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 索引、文档 维护
 *
 * @author MaoLin Wang
 * @date 2020/12/27 12:39 下午
 */
@Service
public class IndexService {

    private Logger logger = LoggerFactory.getLogger(IndexService.class);
    @Value("${maimao.es.goods.index}")
    private String index;

    @Value("${maimao.es.goodsCheck.index}")
    private String goodsCheckIndex;

    @Value("${maimao.es.order.index}")
    private String orderIndex;

    @Value("${maimao.es.goodsCheck.type}")
    private String goodsCheckType;

    @Value("${maimao.es.order.type}")
    private String orderType;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private EsUtil esUtil;


    public void createGoodsIndex() {
        boolean success = false;
        try {
            success = esUtil.createIndex(this.goodsCheckIndex, GOODS_CHECK_MAPPING);
            if (success) {
                logger.info("创建索引:{}成功", index);
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("创建索引:{}失败\nerror=>{}", index, e.getMessage());
        }

    }

    public void createGoodsCheckIndex() {
        boolean success = false;
        try {
            success = esUtil.createIndex(this.goodsCheckIndex, GOODS_CHECK_MAPPING);
            if (success) {
                logger.info("创建索引:{}成功", goodsCheckIndex);
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("创建索引:{}失败\nerror=>{}", goodsCheckIndex, e.getMessage());
        }

    }

    public void createOrderIndex() {
        boolean success = false;
        try {
            success = esUtil.createIndex(this.orderIndex, GOODS_CHECK_MAPPING);
            if (success) {
                logger.info("创建索引:{}成功", index);
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("创建索引:{}失败\nerror=>{}", orderIndex, e.getMessage());
        }

    }

    public void createIndex(String index, String source) {
        boolean success = false;
        try {
            success = esUtil.createIndex(index, source);
            if (success) {
                logger.info("创建索引:{}成功", index);
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("创建索引:{}失败\nerror=>{}", index, e.getMessage());
        }

    }

    public static void main(String[] args) {
        System.out.println(GOODS_MAPPING);
    }


    public static final String ERROR_MESSAGE_MAPPING = "{\n" +
            " \"properties\": {\n" +
            "   \"value\":{\n" +
            "     \"type\": \"text\"\n" +
            "   },\n" +
            "   \"message_type\":{\n" +
            "     \"type\": \"keyword\",\n" +
            "     \"index\": false\n" +
            "   }\n" +
            " }\n" +
            "}";
    private static final String GOODS_MAPPING = "{\n" +
            " \"properties\": {\n" +
            "   \"id\":{\n" +
            "     \"type\": \"keyword\"\n" +
            "   },\n" +
            "   \"key\":{\n" +
            "     \"type\": \"text\",\n" +
            "     \"analyzer\": \"ik_max_word\",\n" +
            "     \"search_analyzer\": \"ik_max_word\"\n" +
            "   },\n" +
            "   \"suggestions\":{\n" +
            "     \"type\": \"completion\",\n" +
            "     \"analyzer\": \"ik_max_word\",\n" +
            "     \"search_analyzer\": \"ik_max_word\"\n" +
            "   },\n" +
            "   \"subName\":{\n" +
            "     \"type\": \"keyword\",\n" +
            "     \"index\": \"false\"\n" +
            "   },\n" +
            "   \"brandId\":{\n" +
            "     \"type\": \"long\"\n" +
            "   },\n" +
            "   \"sellCount\":{\n" +
            "     \"type\": \"keyword\"\n" +
            "   },\n" +
            "   \"cid1\":{\n" +
            "     \"type\": \"integer\"\n" +
            "   },\n" +
            "   \"cid2\":{\n" +
            "     \"type\": \"integer\"\n" +
            "   },\n" +
            "   \"cid3\":{\n" +
            "     \"type\": \"integer\"\n" +
            "   },\n" +
            "   \"shopId\":{\n" +
            "     \"type\": \"long\"\n" +
            "   },\n" +
            "   \"shopName\":{\n" +
            "     \"type\": \"keyword\"\n" +
            "   },\n" +
            "   \"createTime\":{\n" +
            "     \"type\": \"date\"\n" +
            "   },\n" +
            "   \"priceList\":{\n" +
            "      \"type\": \"long\"\n" +
            "   },\n" +
            "   \"goodsSkus\":{\n" +
            "     \"type\": \"keyword\",\n" +
            "     \"index\": false\n" +
            "   },\n" +
            "   \"searchParams\":{\n" +
            "     \"type\": \"object\" \n" +
            "   }\n" +
            " }\n" +
            "}";
    private static final String GOODS_CHECK_MAPPING = "{\n" +
            " \"properties\": {\n" +
            "   \"id\":{\n" +
            "     \"type\": \"keyword\"\n" +
            "   },\n" +
            "   \"goodsId\":{\n" +
            "     \"type\": \"keyword\",\n" +
            "     \"index\":true\n" +
            "   },\n" +
            "   \"brandId\":{\n" +
            "     \"type\": \"keyword\",\n" +
            "     \"index\":true\n" +
            "   },\n" + " " +
            "  \"brandName\":{\n" +
            "     \"type\": \"text\",\n" +
            "     \"analyzer\": \"ik_max_word\",\n" +
            "     \"search_analyzer\": \"ik_max_word\"\n" +
            "   },\n" + "  " +
            " \"logo\":{\n" +
            "     \"type\": \"keyword\"\n" +
            "   },\n" +
            "   \"goodsName\":{\n" +
            "     \"type\": \"text\",\n" +
            "     \"analyzer\": \"ik_max_word\",\n" +
            "     \"search_analyzer\": \"ik_max_word\"\n" +
            "   },\n" +
            "   \"sysUserId\":{\n" +
            "     \"type\": \"keyword\",\n" +
            "     \"index\":true\n" +
            "   },\n" +
            "   \"status\":{\n" +
            "     \"type\": \"keyword\"\n" +
            "   },\n" +
            "   \"deliveryTime\":{\n" +
            "     \"type\": \"date\"\n" +
            "   },\n" +
            "   \"checkTime\":{\n" +
            "     \"type\": \"date\"\n" +
            "   }\n" +
            " }\n" +
            "}";
    /**
     * POST /mm_order/_doc/_mapping
     * {
     *
     *   "properties": {
     *     "id":{
     *       "type": "keyword",
     *       "index": true
     *     },
     *     "userId":{
     *       "type": "keyword"
     *     },
     *     "consignee":{
     *       "type": "text"
     *     },
     *     "mobile":{
     *       "type": "keyword"
     *     },
     *     "sysCouponId":{
     *       "type": "keyword"
     *     },
     *     "integral":{
     *       "type": "double"
     *     },
     *     "useIntegral":{
     *       "type": "boolean"
     *     },
     *     "payCode":{
     *       "type": "keyword"
     *     },
     *     "payTime":{
     *       "type": "date"
     *     },
     *     "createTime":{
     *       "type": "date"
     *     },
     *     "closeTime":{
     *       "type": "date"
     *     },
     *     "status":{
     *       "type": "keyword"
     *     },
     *     "deleted":{
     *       "type": "keyword"
     *     },
     *
     *     "orderShopList": {
     *           "type": "nested",
     *           "properties": {
     *             "id":    {
     *               "type": "keyword"
     *             },
     *             "shopId": {
     *               "type": "keyword"
     *             },
     *             "shopName": {
     *               "type": "keyword"
     *             },
     *             "shopCouponId":     {
     *               "type": "keyword"   },
     *             "deliveryFee":  {
     *               "type": "double"   },
     *             "couponPrice":    {
     *               "type": "double"    },
     *             "totalPrice":{
     *               "type":"double"
     *             },
     *             "actualPrice":{
     *               "type":"double"
     *             },
     *             "status":{
     *               "type":"keyword"
     *             },
     *             "shipSn":{
     *               "type":"keyword"
     *             },
     *             "changeStatus":{
     *               "type":"keyword"
     *             },
     *             "refundId":{
     *               "type":"keyword"
     *             },
     *             "invoice":{
     *               "type":"keyword"
     *             },
     *             "deleted":{
     *               "type":"keyword"
     *             },
     *
     *             "orderGoodsList":{
     *               "type": "nested",
     *               "properties":{
     *                 "id":{
     *                   "type":"keyword"
     *                 },
     *                 "orderShopId":{
     *                   "type":"keyword"
     *                 },
     *                 "comment":{
     *                   "type":"boolean"
     *                 },
     *                 "skuId":{
     *                   "type":"keyword"
     *                 },
     *                 "goodsId":{
     *                   "type":"keyword"
     *                 },
     *                  "title":{
     *                   "type":"text",
     *                   "analyzer":"ik_max_word",
     *                   "search_analyzer":"ik_max_word"
     *                 },
     *                  "num":{
     *                   "type":"keyword"
     *                 },
     *                  "price":{
     *                   "type":"double"
     *                 },
     *                 "brandId":{
     *                   "type":"keyword"
     *                 },
     *                 "shippedStatus":{
     *                   "type":"keyword"
     *                 },
     *                 "receiveTime":{
     *                   "type":"date"
     *                 },
     *                 "integral":{
     *                   "type":"keyword"
     *                 }
     *
     *               }
     *             }
     *
     *           }
     *         }
     *
     *
     *   }
     * }
     */
    private static final String ORDER_MAPPING = "{\n" +
            "  \n" +
            "  \"properties\": {\n" +
            "    \"id\":{\n" +
            "      \"type\": \"keyword\",\n" +
            "      \"index\": true\n" +
            "    },\n" +
            "    \"userId\":{\n" +
            "      \"type\": \"keyword\"\n" +
            "    },\n" +
            "    \"consignee\":{\n" +
            "      \"type\": \"text\"\n" +
            "    },\n" +
            "    \"mobile\":{\n" +
            "      \"type\": \"keyword\"\n" +
            "    },\n" +
            "    \"sysCouponId\":{\n" +
            "      \"type\": \"keyword\"\n" +
            "    },\n" +
            "    \"integral\":{\n" +
            "      \"type\": \"double\"\n" +
            "    },\n" +
            "    \"useIntegral\":{\n" +
            "      \"type\": \"boolean\"\n" +
            "    },\n" +
            "    \"payCode\":{\n" +
            "      \"type\": \"keyword\"\n" +
            "    },\n" +
            "    \"payTime\":{\n" +
            "      \"type\": \"date\"\n" +
            "    },\n" +
            "    \"createTime\":{\n" +
            "      \"type\": \"date\"\n" +
            "    },\n" +
            "    \"closeTime\":{\n" +
            "      \"type\": \"date\"\n" +
            "    },\n" +
            "    \"status\":{\n" +
            "      \"type\": \"keyword\"\n" +
            "    },\n" +
            "    \"deleted\":{\n" +
            "      \"type\": \"keyword\"\n" +
            "    },\n" +
            "    \n" +
            "    \"orderShopList\": {\n" +
            "          \"type\": \"nested\", \n" +
            "          \"properties\": {\n" +
            "            \"id\":    {\n" +
            "              \"type\": \"keyword\"  \n" +
            "            },\n" +
            "            \"shopId\": { \n" +
            "              \"type\": \"keyword\"  \n" +
            "            },\n" +
            "            \"shopName\": { \n" +
            "              \"type\": \"keyword\"  \n" +
            "            },\n" +
            "            \"shopCouponId\":     { \n" +
            "              \"type\": \"keyword\"   },\n" +
            "            \"deliveryFee\":  { \n" +
            "              \"type\": \"double\"   },\n" +
            "            \"couponPrice\":    { \n" +
            "              \"type\": \"double\"    },\n" +
            "            \"totalPrice\":{\n" +
            "              \"type\":\"double\"\n" +
            "            },\n" +
            "            \"actualPrice\":{\n" +
            "              \"type\":\"double\"\n" +
            "            },\n" +
            "            \"status\":{\n" +
            "              \"type\":\"keyword\"\n" +
            "            },\n" +
            "            \"shipSn\":{\n" +
            "              \"type\":\"keyword\"\n" +
            "            },\n" +
            "            \"changeStatus\":{\n" +
            "              \"type\":\"keyword\"\n" +
            "            },\n" +
            "            \"refundId\":{\n" +
            "              \"type\":\"keyword\"\n" +
            "            },\n" +
            "            \"invoice\":{\n" +
            "              \"type\":\"keyword\"\n" +
            "            },\n" +
            "            \"deleted\":{\n" +
            "              \"type\":\"keyword\"\n" +
            "            },\n" +
            "            \n" +
            "            \"orderGoodsList\":{\n" +
            "              \"type\": \"nested\",\n" +
            "              \"properties\":{\n" +
            "                \"id\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                },\n" +
            "                \"orderShopId\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                },\n" +
            "                \"comment\":{\n" +
            "                  \"type\":\"boolean\"\n" +
            "                },\n" +
            "                \"skuId\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                },\n" +
            "                \"goodsId\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                },\n" +
            "                 \"title\":{\n" +
            "                  \"type\":\"text\",\n" +
            "                  \"analyzer\":\"ik_max_word\",\n" +
            "                  \"search_analyzer\":\"ik_max_word\"\n" +
            "                },\n" +
            "                 \"num\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                },\n" +
            "                 \"price\":{\n" +
            "                  \"type\":\"double\"\n" +
            "                }, \n" +
            "                \"brandId\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                }, \n" +
            "                \"shippedStatus\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                }, \n" +
            "                \"receiveTime\":{\n" +
            "                  \"type\":\"date\"\n" +
            "                }, \n" +
            "                \"integral\":{\n" +
            "                  \"type\":\"keyword\"\n" +
            "                }\n" +
            "              \n" +
            "              }\n" +
            "            }\n" +
            "\n" +
            "          }\n" +
            "        }\n" +
            "\n" +
            "\n" +
            "  }\n" +
            "}";
}
